package com.bicon.botu.mall.goods.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.bicon.botu.commons.constants.MallConstants;
import com.bicon.botu.mall.goods.service.IClassAttributeService;
import com.bicon.botu.mall.goods.vo.ClassAttributeVo;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.mall.goods.commons.GoodsConstants;
import com.bicon.botu.mall.goods.entity.ClassAttribute;
import com.bicon.botu.mall.goods.entity.ClassAttributeItem;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 商品规格属性Controller
 * @author songxu
 * @since 2017-07-19
 */
@RestController
@RequestMapping("/V0.0.1/classAttribute")
public class ClassAttributeController extends BaseController{

    @Autowired
    private IClassAttributeService iClassAttributeService;    //商品规格项

    /**
     * 商品规格新增
     * @param
     * @return
     */
    @RequestMapping(value="save",method = RequestMethod.POST)
    public BaseResult save(String paramJson){
        //参数为空校验
        if(StringUtils.isNotBlank(paramJson)){
            //解析数据
            ClassAttributeVo classAttributeVo= JSON.parseObject(paramJson,ClassAttributeVo.class);
            ClassAttribute classAttribute=classAttributeVo.getClassAttribute();
            List<ClassAttributeItem> classAttributeItems=classAttributeVo.getClassAttributeItems();
            //商品规格名称、一级属性、二级属性、分类ID 参数校验
            if(classAttribute!=null && classAttributeItems!=null
                    && classAttributeItems.size()>0
                    && StringUtils.isNotBlank(classAttribute.getClassId())
                    && StringUtils.isNotBlank(classAttribute.getClassName())
                    && StringUtils.isNotBlank(classAttribute.getFirstAttribute())
                    && StringUtils.isNotBlank(classAttribute.getSecondAttribute())){
                int count=iClassAttributeService.selectCount(new EntityWrapper<ClassAttribute>()
                        .eq("class_id",classAttribute.getClassId()).eq("logic_delete",GoodsConstants.DEL_LOGIC_NORMAL));
                if(count>0){
                    return sendResult(MallConstants.RESPONSE_CODE_605,MallConstants.RESPONSE_MESSAGE_605);
                }
                //初始化ID、创建时间、修改时间
                classAttribute.preInsert();
                //商品规格属性项设计初始化ID、创建时间、修改时间、商品规格
                for(ClassAttributeItem entity:classAttributeItems){
                    entity.preInsert();
                    entity.setAttributeId(classAttribute.getId());
                }
                //保存商品规格及对应规格项
                boolean result=iClassAttributeService.saveClassAttributeAndItem(classAttribute,classAttributeItems);
                if(result){
                    return sendResult(GoodsConstants.RESPONSE_CODE_200,GoodsConstants.RESPONSE_MESSAGE_200);
                }else{
                    return sendResult(GoodsConstants.RESPONSE_CODE_600,GoodsConstants.RESPONSE_MESSAGE_600);
                }
            }else{
                return sendResult(GoodsConstants.RESPONSE_CODE_400,GoodsConstants.RESPONSE_MESSAGE_400);
            }
        }else{
            return sendResult(GoodsConstants.RESPONSE_CODE_400,GoodsConstants.RESPONSE_MESSAGE_400);
        }
    }

    /*@RequestMapping(value="save",method = RequestMethod.POST)
    public BaseResult save(ClassAttribute classAttribute){
        if(classAttribute!=null){
            //商品规格名称、一级属性、二级属性、分类ID 参数校验
            if(StringUtils.isNotBlank(classAttribute.getClassId()) && StringUtils.isNotBlank(classAttribute.getClassName())
                    && StringUtils.isNotBlank(classAttribute.getFirstAttribute())
                    && StringUtils.isNotBlank(classAttribute.getSecondAttribute())){
                int count=iClassAttributeService.selectCount(new EntityWrapper<ClassAttribute>()
                        .eq("class_id",classAttribute.getClassId()).eq("logic_delete",GoodsConstants.DEL_LOGIC_NORMAL));
                if(count>0){
                    return sendResult(GoodsConstants.RESPONSE_CODE_605,GoodsConstants.RESPONSE_MESSAGE_605);
                }
                //初始化ID、创建时间、修改时间
                classAttribute.preInsert();
                //商品规格保存或修改 操作数据库
                boolean result=iClassAttributeService.insertOrUpdate(classAttribute);
                if(result){
                    return sendResult(GoodsConstants.RESPONSE_CODE_200,GoodsConstants.RESPONSE_MESSAGE_200);
                }else{
                    return sendResult(GoodsConstants.RESPONSE_CODE_600,GoodsConstants.RESPONSE_MESSAGE_600);
                }
            }else{
                return sendResult(GoodsConstants.RESPONSE_CODE_400,GoodsConstants.RESPONSE_MESSAGE_400);
            }
        }else{
            return sendResult(GoodsConstants.RESPONSE_CODE_400,GoodsConstants.RESPONSE_MESSAGE_400);
        }
    }*/

    /**
     * 商品规格修改
     * @param classAttribute
     * @return
     */
    @RequestMapping(value="modify",method = RequestMethod.POST)
    public BaseResult modify(ClassAttribute classAttribute){
        if(classAttribute!=null){
            //商品规格名称、一级属性、二级属性、分类ID 参数校验
            if(StringUtils.isNotBlank(classAttribute.getId())){
                //校验主键是否为空 为空则创建
                classAttribute.setUpdateDate(new Date());
                //商品规格保存或修改 操作数据库
                boolean result=iClassAttributeService.insertOrUpdate(classAttribute);
                if(result){
                    return sendResult(GoodsConstants.RESPONSE_CODE_200,GoodsConstants.RESPONSE_MESSAGE_200);
                }else{
                    return sendResult(GoodsConstants.RESPONSE_CODE_600,GoodsConstants.RESPONSE_MESSAGE_600);
                }
            }else{
                return sendResult(GoodsConstants.RESPONSE_CODE_200,GoodsConstants.RESPONSE_MESSAGE_200);
            }
        }else{
            return sendResult(GoodsConstants.RESPONSE_CODE_400,GoodsConstants.RESPONSE_MESSAGE_400);
        }
    }

    /**
     * 商品规格删除
     * @param id
     * @return
     */
    @RequestMapping(value="delete/{id}",method = RequestMethod.GET)
    public BaseResult delete(@PathVariable("id") String id){
        boolean result=false;
        result=iClassAttributeService.deleteClassAttributeAndItem(id);
        if(result){
            return sendResult(GoodsConstants.RESPONSE_CODE_200,GoodsConstants.RESPONSE_MESSAGE_200);
        }else{
            return sendResult(GoodsConstants.RESPONSE_CODE_600,GoodsConstants.RESPONSE_MESSAGE_600);
        }
    }

    /**
     * 商品规格分页
     * @param pageNo
     * @param pageSize
     * @param classAttribute
     * @return
     */
    @RequestMapping(value="findPage",method = RequestMethod.POST)
    public BaseResult findPage(@RequestParam(defaultValue = "0") Integer pageNo,
                               @RequestParam(defaultValue = "10") Integer pageSize,
                               ClassAttribute classAttribute){
        Page<ClassAttribute> page=iClassAttributeService.selectPage(new Page<ClassAttribute>(pageNo,pageSize),
                new EntityWrapper<ClassAttribute>(classAttribute).orderBy("create_date",false));
        return sendResult(GoodsConstants.RESPONSE_CODE_200,GoodsConstants.RESPONSE_MESSAGE_200,page);
    }

}
